<?php

declare(strict_types=1);

namespace app\model;

use app\common\facade\supplier\Supplier;
use app\model\base\BaseCouponOrderSidModel;
use app\common\facade\auth\Auth;

/**
 * 
 *
 * @mixin \think\Model
 * @property int $id
 * @property int $agent_id 代理
 * @property int $user_id 用户
 * @property int $channel_id 渠道
 * @property string $create_time
 * @property string $update_time
 * @property int $settle_type 下单时结算类型1返佣2供应商接口
 * @property int $settle_discount 下单时结算折扣
 * @property string $act_id 活动标识
 * @property int $income_type agent_service_config 表设置的类型 1返佣，2接口
 * @property int $serv_type 服务
 * @property string $sid 代理的sid
 * @property string $remark 代理的sid
 * @property string $user_share_openid mobile_user 表中的openid，分享者openid
 * @property string $from 下单来源 h5 applet 小程序
 */
class CouponOrderSid extends BaseCouponOrderSidModel
{
    const REMARK_JX = 'jx';


    const FROM_H5 = 'h5';
    const FROM_APPLET = 'applet';//小程序

    /**
     * 生成SID
     * @param $agent_id
     * @param $channel_id
     * @param $user_id
     * @return int
     */
    public static function generate($agent_id, $channel_id, $user_id, $act_id = 0, $serv_type = 0)
    {
        $params = request()->all();
        $user_share_openid = input('user_share_openid', '');
        /**
         *
         */
        $supplier = Supplier::getBaseSupplier();

        $income_type = AgentServiceConfig::INCOME_TYPE_REBATE;

        if ($supplier) {
            if (!empty($supplier->agent_id)) {
                $income_type = AgentServiceConfig::INCOME_TYPE_SELF;
            }
        }

        $obj = CouponOrderSid::findOrCreate([
            'agent_id' => $agent_id,
            'channel_id' => $channel_id,
            'user_id' => $user_id,
            'act_id' => $act_id,
            'income_type' => $income_type,
            'serv_type' => $serv_type,
            'user_share_openid' => $user_share_openid, //分享者id
            'from' => $params['app_from'] ?? self::FROM_H5,
        ]);

//
//        $obj = new CouponOrderSid();
//        $obj->agent_id = $agent_id;
//        $obj->channel_id = $channel_id;
//        $obj->user_id = $user_id;
//        $obj->act_id = $act_id;
//        $obj->income_type = $income_type;
//        $obj->serv_type = $serv_type;
//        $obj->save();

        return $obj->id;
    }

    /**
     * 生成SID(新)
     * @param $agent_id
     * @param $channel_id
     * @param $user_id
     * @return int
     * @author ymzhang
     */
    public function generateNew($serv_type = 0, $remark = '')
    {
        $params = request()->all();

        $user_share_openid = input('user_share_openid', '');

        $supplier = Supplier::getBaseSupplier();

        $income_type = AgentServiceConfig::INCOME_TYPE_REBATE;

        if ($supplier) {
            if (!empty($supplier->agent_id)) {
                $income_type = AgentServiceConfig::INCOME_TYPE_SELF;
            }
        }
        /**
         * @var CouponOrderSid $obj
         */
        $obj = CouponOrderSid::findOrCreate([
            'agent_id' => Auth::agentId(),    //代理id
            'channel_id' => Auth::channelId(),  //渠道id
            'user_id' => Auth::id(),         //用户id
            'income_type' => $income_type,       //设置类型，1返佣 2接口
            'serv_type' => $serv_type,         //供应商类型
            'remark' => $remark,            //渠道备注
            'user_share_openid' => $user_share_openid, //分享者id
            'from' => $params['app_from'] ?? self::FROM_H5,
        ], [], true);
        return $obj->id;
    }
}
